package com.xjiuge.fruit.controller.manage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xjiuge.fruit.bean.Role;
import com.xjiuge.fruit.bean.RoleMenu;
import com.xjiuge.fruit.bean.User;
import com.xjiuge.fruit.common.JsonResult;
import com.xjiuge.fruit.common.Response;
import com.xjiuge.fruit.common.ResponseCodeConstant;
import com.xjiuge.fruit.common.ValidateUtil;
import com.xjiuge.fruit.service.RoleService;

@Controller("RoleController")
@RequestMapping("/role")
public class RoleController {

	public final static Logger LOG = Logger.getLogger(RoleController.class);
	
	@Resource(name = "roleService")
	private RoleService roleService;
	
	/**
	 * 添加角色
	 * RoleController.addRole<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年11月7日  下午3:40:23</p>
	 * @param role
	 * @param httpSession
	 * @return
	 */
	@RequestMapping(value = "/addRole",method = RequestMethod.POST)
	@ResponseBody
	public JsonResult addRole(Role role,HttpSession httpSession,String [] ids){
		Integer code = null;
		String msg = "";
		Response response = new Response();
		if(ValidateUtil.isNull(role.getRoleName())){
			code = ResponseCodeConstant.Common.Business_exception.getCode();
			msg = "角色名称不能为空";
			response.setCode(code);
			response.setMsg(msg);
			return new JsonResult(response);
		}
		if(ValidateUtil.isNullForArray(ids) || 0 == ids.length){
			code = ResponseCodeConstant.Common.Business_exception.getCode();
			msg = "菜单ID数组不能为空";
			response.setCode(code);
			response.setMsg(msg);
			return new JsonResult(response);
		}
		try {
			User user = (User) httpSession.getAttribute("user");
			roleService.save(role,user,ids);
			code = ResponseCodeConstant.Common.success.getCode();
			msg = ResponseCodeConstant.Common.success.getValue();
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "添加角色异常"+e;
			LOG.error("添加角色异常",e);
		}
		response.setCode(code);
		response.setMsg(msg);
		return new JsonResult(response);
	}
	
	/**
	 * 查询所有角色列表
	 * RoleController.selectAllRole<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年11月7日  下午3:59:41</p>
	 * @return
	 */
	@RequestMapping(value = "/selectAllRole",method = RequestMethod.POST)
	@ResponseBody
	public JsonResult selectAllRole(){
		Integer code = null;
		String msg = "";
		Response response = new Response();
		Map<String, Object> data = new HashMap<>();
		try {
			List<RoleMenu> roleList = roleService.selectAllRole();
			code = ResponseCodeConstant.Common.success.getCode();
			msg = ResponseCodeConstant.Common.success.getValue();
			data.put("roleList", roleList);
		} catch (Exception e) {
			code = ResponseCodeConstant.Common.error.getCode();
			msg = "查询所有角色列表异常"+e;
			LOG.error("查询所有角色列表异常",e);
		}
		response.setCode(code);
		response.setMsg(msg);
		response.setData(data);
		return new JsonResult(response);
	}
}
